<template>
  <div>
    <tableComponents
      :pagination="{ currentPage, pageSize, total: list.all_num }"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="receive_search"
      :searchData="userSearchData"
      @toAdd="add_data"
      exportUrl="/api/admin/language_info/excel_dc"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          v-loading="loading"
          element-loading-text="拼命加载中"
          stripe
          header-row-class-name="head_color"
          @selection-change="handleSelectionChange"
          tooltip-effect="dark"
          :default-sort="{ prop: 'sort' }"
          style="width: 99.8%"
        >
          <el-table-column
            type="selection"
            class="tc"
            width="35"
          ></el-table-column>
          <el-table-column label="系统文字" min-width="220">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="语言" min-width="220">
            <template slot-scope="scope">
              <div v-for="(item, i) in scope.row.language" :key="i">
                <b>{{ item.lan }}：</b>
                <span>{{ item.val }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="220">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="editbtn(scope.row)"
                >编辑</el-button
              >
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="deletebtn(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:headRight>
        <el-button
          :loading="$store.state.isLoading"
          type="success"
          size="mini"
          @click="update"
          >更 新</el-button
        >
      </template>
      <template v-slot:footerLeft>
        <el-button
          :loading="$store.state.isLoading"
          type="primary"
          size="mini"
          @click="deleteAll"
          >删 除</el-button
        >
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="编辑"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="live_single" label-width="120px" ref="form">
        <el-form-item label="系统名称：">
          <el-col :span="12">
            <el-input
              type="text"
              :rows="6"
              clearable
              placeholder="请输入"
              v-model="live_single.title"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item
          :label="item.title + '：'"
          v-for="(item, i) in cate_option"
          :key="i"
        >
          <el-col :span="12">
            <el-input
              type="text"
              :rows="6"
              clearable
              placeholder="请输入"
              v-model="live_single[item.iden]"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            type="primary"
            @click="editlive(live_single)"
            >编 辑</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <keep-alive>
      <component
        :is="'xzuser'"
        :show="shows"
        :isAll="false"
        showType="list"
        @sendShop="getSend"
        @isshow="getShow"
      ></component>
    </keep-alive>
  </div>
</template>
<script>
import xzuser from "@/components/currency/xz_user.vue";
import { languagelistSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      cate_option: [],
      userSearchData: languagelistSearchData,
      current: "",
      shows: false,
      is_edit: false,
      dialogFormVisible: false,
      live_single: {
        piclink: "",
      },
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      row: {},
      multipleSelection: [],
      showCity: true,
    };
  },
  components: {
    xzuser,
  },
  methods: {
    update() {
      this.$confirm(`即将更新, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.getPage.update_language({})((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "更新成功",
              });
            } else {
              this.$message({
                type: "info",
                message: res.data.result,
              });
            }
          });
        })
        .catch(() => {});
    },
    get_type() {
      this.loading = true;
      this.$api.getPage.get_languagetype({}).then((res) => {
        if (res.data.code == 200) {
          this.cate_option = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    getShow(val) {
      this.shows = val;
    },
    getSend(row) {
      this.live_single.username = row.username;
      this.live_single.uid = row.id;
    },
    xzshop() {
      this.shows = true;
    },
    sorta(a, b) {
      return b - a;
    },
    sortNew(index, row) {
      var params = {};
      params = row;
      params.id = row.id;
      params.sort = row.sort;
      this.$api.getPage.edit_commentlist(params).then((res) => {
        if (res.data.code == 200 && res.data.result) {
          return;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteAll() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            this.$api.getPage.del_languagelist({ id_str: str }).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", res.data.result, "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", res.data.result, "success");
                this.getNewsAll(this.searchAll);
              }
            });
          }
        });
      }
    },
    deletebtn(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getPage.del_languagelist({ id_str: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            }
          });
        }
      });
    },
    add_data() {
      this.live_single = {
        title: "",
      };
      this.is_edit = false;
      this.dialogFormVisible = true;
    },
    onSuccess(pic) {
      console.log(pic);
      this.live_single.piclink = pic;
    },
    getIs(val, row) {
      this.is_edit = true;
      var params = {};
      params = row;
      params.id = row.id;
      params.show = val;
      this.editlive(params);
    },

    editlive(item) {
      var params = {};
      if (this.is_edit == false) {
        params = item;
        params.vid = this.$route.query.id;
        this.$api.getPage.add_languagelista(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        params = item;
        params.vid = this.$route.query.id;
        this.$api.getPage.edit_languagelist(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    editbtn(item) {
      this.is_edit = true;
      // var obj = JSON.parse(JSON.stringify(item));
      var obj = {
        id: item.id,
        title: item.title,
      };
      item.language.forEach((item, i) => {
        obj[item.iden] = item.val;
      });

      this.live_single = obj;
      this.dialogFormVisible = true;
    },

    receive_search(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      params.vid = this.$route.query.id;
      this.$api.getPage.get_languagelist(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },

    reload() {
      this.getNewsAll(this.searchAll);
    },
  },
  beforeCreate() {},
  created() {
    this.get_type();
    this.getNewsAll();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
